
package com.netease.xone.fragment;

import protocol.XoneCallBack;
import protocol.XoneService;
import protocol.XoneServiceCode;
import protocol.meta.User;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.handmark.pulltorefresh.library.X1LoadingAdapterViewBase.OnLoadingListener;
import com.netease.pkgRelated.XoneConsts;
import com.netease.xone.R;
import com.netease.xone.adapter.FansListAdapter;
import com.netease.xone.widget.listview.LoadingListView;

public class FragmentFansList extends FragmentBase {

    public enum FansListType {
        fans, follow
    }

    public static FragmentFansList newInstance(String userId, FansListType type) {
        FragmentFansList f = new FragmentFansList();
        Bundle b = new Bundle();
        b.putString(XoneConsts.BUNDLE_KEY.USER_ID, userId);
        b.putSerializable(XoneConsts.BUNDLE_KEY.FANS_LIST_TYPE, type);
        f.setArguments(b);
        return f;
    }

    // Data
    private String mUserId;
    private FansListType mType;
    // View
    private LoadingListView mList;
    private FansListAdapter mAdapter;
    // Tid
    private int mGetFansListTid;
    private int mGetFollowListTid;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        Bundle b = getArguments();
        mUserId = b.getString(XoneConsts.BUNDLE_KEY.USER_ID);
        mType = (FansListType)b.getSerializable(XoneConsts.BUNDLE_KEY.FANS_LIST_TYPE);
        XoneService.getInstance().addListener(mCallBack);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fans_list, null);
        mList = (LoadingListView)view.findViewById(R.id.list);
        mAdapter = new FansListAdapter(getActivity());
        mList.setAdapter(mAdapter);
        mList.disablePullToRefresh();
        mList.setOnLoadingListener(new OnLoadingListener() {

            @Override
            public void onRefreshing() {

            }

            @Override
            public void onLoading() {
                switch (mType) {
                    case fans:
                        mGetFansListTid = XoneService.getInstance().doGetFansList(mUserId, 20);
                        break;
                    case follow:
                        mGetFollowListTid = XoneService.getInstance().doGetFollowList(mUserId, 20);
                        break;
                }
            }

            @Override
            public void onLoadingMore() {
                switch (mType) {
                    case fans:
                        mGetFansListTid = XoneService.getInstance().doGetFansList(mUserId, 20);
                        break;
                    case follow:
                        mGetFollowListTid = XoneService.getInstance().doGetFollowList(mUserId, 20);
                        break;
                }
            }
        });
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mList.load();
    }

    @Override
    public void onDestroy() {
        XoneService.getInstance().removeListener(mCallBack);
        super.onDestroy();
    };

    XoneCallBack mCallBack = new XoneCallBack() {

        @Override
        public void onGetFansList(int transactionId, java.util.List<protocol.meta.User> userList) {
            if (mGetFansListTid == transactionId) {
                if(userList != null && userList.size() != 0){
                    mAdapter.addTail(userList);
                    mList.onLoadingComplete(false);
                }else{
                    mList.onNoContent(getString(R.string.success_fans_no_fans));
                }
            }
        };

        @Override
        public void onGetFollowList(int transactionId, java.util.List<protocol.meta.User> userList) {
            if (mGetFollowListTid == transactionId) {
                if(userList != null && userList.size() != 0){
                    mAdapter.addTail(userList);
                    mList.onLoadingComplete(false);
                }else{
                    mList.onNoContent(getString(R.string.success_follows_no_follows));
                }
            }
        };

        @Override
        public void onGetFansListError(int transactionId, int errCode, String err) {
            if(mGetFollowListTid == transactionId){
                if(errCode == XoneServiceCode.NETWORK_ERR_COMMON){
                    mList.onNoNetwork();
                }else{
                    mList.onLoadingError();
                }
            }
        };

        @Override
        public void onGetFollowListError(int transactionId, int errCode, String err) {
            if(mGetFollowListTid == transactionId){
                if(errCode == XoneServiceCode.NETWORK_ERR_COMMON){
                    mList.onNoNetwork();
                }else{
                    mList.onLoadingError();
                }
            }
        };

        @Override
        public void onFollow(int transactionId, String userId) {
            for(int i = 0; i < mAdapter.getCount(); i++){
                User user = mAdapter.getItem(i);
                if(user.userId == userId){
                    user.follow = 1;
                    mAdapter.notifyDataSetChanged();
                    break;
                }
            }
        };

        @Override
        public void onCancelFollow(int transactionId, String userId) {
            for(int i = 0; i < mAdapter.getCount(); i++){
                User user = mAdapter.getItem(i);
                if(user.userId == userId){
                    user.follow = 0;
                    mAdapter.notifyDataSetChanged();
                    break;
                }
            }
        };
    };

}
